package org.ogv.usada.models;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.utils.Json;


public class Content {
	//Singleton
	private static Content _$;
	public static Content $() { 
		if(_$ == null) {
			restart();
		}
		
		return _$;
	}

	private Content() {}

	public CustomerDef[] 	customers;
	public LeafDef[] 		leaves;
	public RecipeDef[] 		recipes;
	public IllnessDef[] 	illnesses;
	public LevelDef[] 		levels;
	public PotDef[]			pots;
	public int[]			playerScores;
	public int				latestLevel;
	private Preferences		_pref;
	
	public void updateScore(int idx, int score) {
		if(score > playerScores[idx]) {
			playerScores[idx] = score;
			_pref.putInteger("" + idx, score);
			if(idx + 1 > latestLevel) {
				latestLevel = Math.min(idx + 1, levels.length-1);
				_pref.putInteger("latestlevel", latestLevel);
			}
			_pref.flush();
		}
	}
	
	public static void restart() {
		_$ = new Json().fromJson(Content.class, Gdx.files.internal("data/usada.ctn"));
		_$.loadPlayerScores();
	}
	
	private void loadPlayerScores() {
		_pref 		= Gdx.app.getPreferences("player0");
		playerScores= new int[levels.length];
		for(int i = 0; i < levels.length; ++i) {
			playerScores[i] = _pref.getInteger("" + i);
		}
		latestLevel	= _pref.getInteger("latestlevel");
	}

	public boolean hasPassed(int lvIdx) {
		return levels[lvIdx].getScoreCategory(playerScores[lvIdx]) > 0;
	}
}
